package main

/**
defer 匿名函数的捕获panic应用
多个协程可能会有panic 导致整个程序崩溃
defer函数 + err:recore() 捕获panic
*/

import (
	"fmt"
	"time"
)

func main() {
	go onlyInChan()
	go onlyOutChan(2)
	fmt.Println("exit main")
	time.Sleep(time.Second * 2)
}

func onlyInChan() {
	for i := 0; i < 10; i++ {
		fmt.Println(i)
	}
}

func onlyOutChan(num int) {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("onlyOutChan方法panic:", err)
		}
	}()
	var testMap map[int]int // panic: assignment to entry in nil map
	testMap[0] = num
}
